package top.littlejiang.onlinexam.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.littlejiang.onlinexam.common.annotation.Log;
import top.littlejiang.onlinexam.common.utils.Result;
import top.littlejiang.onlinexam.entity.OlexamRole;
import top.littlejiang.onlinexam.service.OlexamRoleService;
import top.littlejiang.onlinexam.service.OlexamSysService;

import java.io.Serializable;
import java.util.List;


/**
 * @author chen
 * @Description
 * @since 2021/3/6 15:29
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    OlexamRoleService roleService;
    @Autowired
    OlexamSysService sysService;

    /**
     * 查询所有未禁用的角色
     * @return
     */
    @PostMapping("/getnormal")
    @ResponseBody
    public Result getAll(){
        return Result.ok(roleService.getAll(null, true));
    }

    /**
     * 根据角色名模糊查询所有数据
     * @param role
     * @return
     */
    @PostMapping("/getall")
    @ResponseBody
    public Result getAll(@RequestBody OlexamRole role){
        return Result.ok(roleService.getAll(role, false));
    }

    /**
     * 禁用
     * @param ids
     * @return
     */
    @Log("禁用角色")
    @PostMapping("/logicdel")
    @ResponseBody
    @RequiresPermissions("olexam:sys:role")
    public Result delete(@RequestBody List<Serializable> ids){
        roleService.logicDeleteById(ids);
        return Result.ok();
    }

    /**
     * 保存或更新
     * @param role
     * @return
     */
    @Log("新增/更新角色")
    @PostMapping("/saveorupdate")
    @ResponseBody
    @RequiresPermissions("olexam:sys:role")
    public Result update(@RequestBody OlexamRole role){
        sysService.updateOrInsertRole(role);
        return Result.ok();
    }
}
